/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-27 02:38:44  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import com.joysuch.wwyt.bp.bean.BpDeviceFacilityListBean;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityListSearchBean;
import com.joysuch.wwyt.bp.entity.BpDeviceFacilityCollection;
import com.joysuch.wwyt.bp.service.BpDeviceFacilityCollectionService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "设备设施管理-设备检查")
@RequestMapping("/bp/device-facility")
public class BpDeviceFacilityCollectionController {

    private static final Logger logger = LoggerFactory.getLogger(BpDeviceFacilityCollectionController.class);

    @Autowired
    private BpDeviceFacilityCollectionService bpDeviceFacilityCollectionService;

    @PostMapping("add")
    public Object add(@Valid BpDeviceFacilityCollection entity) {
        try {
            bpDeviceFacilityCollectionService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @PostMapping("update")
    public Object update(@Valid BpDeviceFacilityCollection entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpDeviceFacilityCollectionService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    /**
     * 设备设施列表，分页查询接口
     * 
     * @param page
     *            分页参数，
     * @param condition
     *            查询参数
     * @return 分页查询结果
     */
    @GetMapping("page")
    public Object page(Pageable page, BpDeviceFacilityListSearchBean condition) {
        try {

            Page<BpDeviceFacilityListBean> data = bpDeviceFacilityCollectionService.findByPage(page, condition);
            return ResultBean.pageData(data);
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    public Object delete(Long id) {
        try {
            bpDeviceFacilityCollectionService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

}
